import { _decorator, Component, EventTouch, instantiate, Node, UITransform } from 'cc';
import { Food } from '../Food';
const { ccclass, property } = _decorator;

@ccclass('FoodMachine')
export class FoodMachine extends Component {

    @property(Node)
    point: Node;

    @property(Node)
    cloneFood: Node;

    @property(UITransform)
    process: UITransform;

    food: Food;

    start() {
        this.point.on(Node.EventType.TOUCH_START, this.TouchStart, this);
        this.point.on(Node.EventType.TOUCH_MOVE, this.TouchMove, this);
        this.point.on(Node.EventType.TOUCH_END, this.TouchEnd, this);
        this.point.on(Node.EventType.TOUCH_CANCEL, this.TouchEnd, this);
    }

    update(deltaTime: number) {
    }

    TouchStart(event: EventTouch) {
        // return;
        let node = instantiate(this.cloneFood);
        node.active = true;
        node.setWorldPosition(this.point.getWorldPosition());
        this.food = node.getComponent(Food);
        this.food.process = this.process;
        this.food.lastPos = this.food.node.getPosition();
        this.food.TouchStart(event);
    }

    TouchMove(event: EventTouch) {
        // console.log("执行", event.target.name);
        // return;
        this.food.TouchMove(event);
    }

    TouchEnd(event: EventTouch) {
        // return;
        this.food.TouchEnd(event);
        this.food = null;
    }
}


